package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"zk/config"
	"zk/global"
	"zk/internal/model"
)

var err error

func Mysql_Init() {
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	add := config.Add.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", add.User, add.Password, add.Host, add.Port, add.Data)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("mysql is success")
	global.DB.AutoMigrate(&model.User{}, &model.LiveRoom{}, &model.Gift{}, &model.UserInteraction{})
}
